package com.caoji.item.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caoji.advice.lyExcption;
import com.caoji.dto.PageDTO;
import com.caoji.entity.BrandDTO;
import com.caoji.item.entity.Brand;
import com.caoji.item.entity.CategoryBrand;
import com.caoji.item.mapper.BrandMapper;
import com.caoji.item.service.BrandService;
import com.caoji.item.service.CategoryBrandService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Autowired
    CategoryBrandService categoryBrandService;
    @Override
    public List<BrandDTO> findbyids(List<Long> ids) {
        return BrandDTO.convertEntityList(listByIds(ids));
    }

    @Override
    public List<BrandDTO> findbybanid(Long id) {
        List<Long> banis = baseMapper.findbycateid(id);
        List<BrandDTO> findbyids = findbyids(banis);
        if(CollectionUtils.isEmpty(findbyids)){
            throw new lyExcption(400,"查询失败");
        }
        return findbyids;
    }

    @Override
    public BrandDTO findbybanids(Long id) {
        return new BrandDTO(baseMapper.selectById(id));
    }

    @Override
    public PageDTO<BrandDTO> findbypage(String key, Long page, Long rows) {
        boolean isexit = StringUtils.isNoneBlank(key);
        Page<Brand> info = new Page<>(page, rows);
         query().like(isexit,"name",key)
                 .or()
                 .eq(isexit,"letter",key).page(info);
        return new PageDTO<>(info.getTotal(),info.getPages(),BrandDTO.convertEntityList(info.getRecords()));
    }

    @Override
    @Transactional
    public void savebrand(BrandDTO brandDTO) {
        Brand brand = brandDTO.toEntity(Brand.class);
        save(brand);
        List<CategoryBrand> collect = brandDTO.getCategoryIds().stream()
                .map(id -> CategoryBrand.of(id, brand.getId())).collect(Collectors.toList());
        categoryBrandService.saveBatch(collect);
    }

    @Override
    @Transactional
    //先逻辑删除 再彻底删除
    public void deletebyid(Long id) {
       removeById(id);
       categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id",id));
    }

    @Override
    @Transactional
    public void updateall(BrandDTO brandDTO) {
        Brand brand = brandDTO.toEntity(Brand.class);
        boolean b = updateById(brand);
        if(!b){
            throw  new lyExcption(500,"更新失败");
        }else {
            boolean remove = categoryBrandService.remove(new QueryWrapper<CategoryBrand>().eq("brand_id", brand.getId()));
            if(!remove){
                throw  new lyExcption(500,"中间表删除失败");
            }else {
                List<CategoryBrand> collect = brandDTO.getCategoryIds().stream()
                        .map(id -> CategoryBrand.of(id, brand.getId())).collect(Collectors.toList());
                categoryBrandService.saveBatch(collect);
            }
        }
    }
}